package com.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		// HttpServletResponse httpResponse = (HttpServletResponse) response;
		HttpSession session = httpRequest.getSession();
		// 跳转到 登陆url
		String loginUrl = httpRequest.getContextPath() + "/login";
		String url = httpRequest.getRequestURI();
		System.out.println(url.lastIndexOf("."));
		// 超时处理，ajax请求超时设置超时状态，页面请求超时则返回提示并重定向
		if (session.getAttribute("username") == null && url.lastIndexOf(".") == -1) {
			if (url != null && !"".equals(url) && url.indexOf("login") >= 0) {
				// 如果本身是登录，则什么也不执行
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/basicsData/getBasicsData") >= 0 || url.indexOf("/qrcode/getQrcodeInfo") >= 0) {
				//qrcode/getQrcodeInfo  /basicsData/getBasicsData
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/electric/getElectric") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/pjinfo/getPjInfo") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/construction/getConstruction") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/registration/getMaintenance") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/registration/getRegistration") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/registrationRecord/getRegistrationRecordByDeviceId") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/registrationRecord/getRegistrationRecordByDeviceIdXj") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/registrationRecord/getRegistrationRecordByDeviceIdWx") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/uploadfile") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/internal/getInternal") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/basicsData/getBasicsData") >= 0) {
				chain.doFilter(httpRequest, response);
			}else if(url.indexOf("/internalInfo/getInternalInfo") >= 0) {
				chain.doFilter(httpRequest, response);
			}
			///maintenance/getMaintenance
			// 判断是否为ajax请求
			else if (httpRequest.getHeader("x-requested-with") != null
					&& httpRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
				chain.doFilter(request, response);// 不可少，否则请求会出错
			} else {
				StringBuffer str = new StringBuffer("");
				str.append("<script type=\"text/javascript\" src=\"" + httpRequest.getContextPath()
						+ "/layui/layui.all.js\"></script>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\""
						+ httpRequest.getContextPath() + "/layui/css/layui.css\">");
				str.append("<script type=\"text/javascript\" src=\"" + httpRequest.getContextPath()
						+ "/js/layui_common.js\"></script>");
				str.append("<script language='javascript'>layer.alert('会话过期,请重新登录');" + "window.top.location.href='"
						+ loginUrl + "';</script>");
				response.setContentType("text/html;charset=UTF-8");// 解决中文乱码
				try {
					PrintWriter writer = response.getWriter();
					writer.write(str.toString());
					writer.flush();
					writer.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} else if (url.lastIndexOf(".") >= 0) {
			chain.doFilter(httpRequest, response);
		} else {
			chain.doFilter(httpRequest, response);
		}
	}

	@Override
	public void destroy() {

	}

}
